<?php
/**
 * (C)2012-2014 twcms.com TongWang Inc.
 * Author: wuzhaohuan <kongphp@gmail.com>
 */

defined('TWCMS_PATH') or exit;

class user_control extends admin_control {

    public function __construct()
    {
        parent::__construct();

        if ($this->_user['groupid'] !== '1') {
            $this->message(0, '对不起，您所在的用户组无权访问', -1);
        }
    }

    // 用户管理
	public function index() {
        // hook admin_user_control_index_before.php

        $keyword = empty($_POST) ? R('keyword') : R('keyword', 'P');
        if($keyword) {
            $keyword = urldecode($keyword);
            $keyword = safe_str($keyword);
        }
        $this->assign('keyword', $keyword);

        // 初始模型表名
        $this->user->table = 'user';

        // 初始分页
        $pagenum = 20;
        if($keyword) {
            $where = array('username'=>array('LIKE'=>$keyword));
            $total = $this->user->find_count($where);
            $urlstr = '-keyword-'.urlencode($keyword);
        }
        else {
            $where = array();
            $total = $this->user->count();
            $urlstr = '';
        }
        $maxpage = max(1, ceil($total/$pagenum));
        $page = min($maxpage, max(1, intval(R('page'))));
        $pages = pages($page, $maxpage, 'index.php?u=user-index'.$urlstr.'-page-{page}');
        $this->assign('total', $total);
        $this->assign('pages', $pages);

        // 读取内容列表
        $user_arr = $this->user->list_arr($where, 'uid', -1, ($page-1)*$pagenum, $pagenum, $total);

        $this->assign('user_arr', $user_arr);

		// hook admin_user_control_index_after.php
		$this->display();
	}

    // 新用户
    public function add() {
        // hook admin_user_control_add_before.php

        if(empty($_POST)) {
            $this->_pkey = 'uid';
            $this->_ukey = 'user-add';
            $this->_title = '新增用户';
            $this->_place = '用户 &#187; 用户管理 &#187 新增用户';

            $data = [];
            $this->assign('data', $data);

            $gidhtml = $this->user_group->get_html_by_gid(0);
            $this->assign('gidhtml', $gidhtml);

            $this->display('user_set.htm');
        }else{
            $gid = intval(R('gid', 'P'));
            $username = trim(R('username', 'P'));
            $password = trim(R('password', 'P'));

            empty($gid) && E(1, '亲，您没有选择群组哦！');
            empty($username) && E(1, '亲，您的用户名忘了填哦！');
            empty($password) && E(1, '亲，您的密码忘了填哦！');

            $user_group = $this->user_group->read($gid);
            if(empty($user_group)) E(1, '群组不存在！');

            // 密码干扰字符
            $salt = random(16, 3, '0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>,.');

            // 写入内容表
            $data = array(
                'groupid' => $gid,
                'username' => $username,
                'password' => md5(md5($password).$salt),
                'salt' => $salt,
                'email' => trim(R('email', 'P')),
                'regip' => ip2long(ip()),
                'regdate' => time(),
            );
            $this->user->table = 'user';
            $id = $this->user->create($data);
            if(!$id) {
                E(1, '写入用户表出错');
            }

            // hook admin_user_control_add_after.php

            E(0, '新增成功');
        }
    }

    // 编辑用户
    public function edit() {
        // hook admin_user_control_edit_before.php

        $this->user->table = 'user';

        if(empty($_POST)) {
            $uid = intval(R('uid'));

            $this->_pkey = 'uid';
            $this->_ukey = 'user-edit-uid-'.$uid;
            $this->_title = '编辑用户';
            $this->_place = '用户 &#187; 用户管理 &#187 编辑用户';

            // 读取用户
            $data = $this->user->get($uid);
            if(empty($data)) {
                $this->message(0, '用户不存在！', -1);
            }

            $data['regdate'] = date('Y-m-d H:i:s', $data['regdate']);
            $this->assign('data', $data);

            $gidhtml = $this->user_group->get_html_by_gid($data['groupid']);
            $this->assign('gidhtml', $gidhtml);

            $this->display('user_set.htm');
        }else{
            $uid = intval(R('uid', 'P'));
            $gid = intval(R('gid', 'P'));
            $username = trim(R('username', 'P'));
            $password = trim(R('password', 'P'));

            empty($uid) && E(1, 'ID不能为空！');
            empty($gid) && E(1, '亲，您没有选择群组哦！');
            empty($username) && E(1, '亲，您的用户名忘了填哦！');
            empty($password) && E(1, '亲，您的密码忘了填哦！');

            $user_group = $this->user_group->read($gid);
            if(empty($user_group)) E(1, '群组不存在！');

            $data = $this->user->get($uid);
            if(empty($data)) E(1, '用户不存在！');

            if ($data['password'] !== $password) {
                $data['password'] = md5(md5($password).$data['salt']);
            }

            // 写入内容表
            $data['uid'] = $uid;
            $data['groupid'] = $gid;
            $data['username'] = $username;
            $data['email'] = trim(R('email', 'P'));
            $data['intro'] = trim(R('intro', 'P'));

            if(!$this->user->update($data)) {
                E(1, '更新内容表出错');
            }

            // hook admin_user_control_edit_after.php

            E(0, '编辑完成');
        }
    }

    // 删除用户
    public function del() {
        // hook admin_user_control_del_before.php

        $uid = (int) R('uid', 'P');

        empty($uid) && E(1, '用户ID不能为空！');

        $err = $this->user->delete($uid);

        // hook admin_user_control_del_after.php

        if($err) {
            E(0, '删除成功！');
        }else{
            E(1, '删除失败！');
        }
    }

    // 批量删除用户
    public function batch_del() {
        // hook admin_user_control_batch_del_before.php

        $id_arr = R('id_arr', 'P');
        if(!empty($id_arr) && is_array($id_arr)) {
            $err_num = 0;
            foreach($id_arr as $v) {
                $err = $this->user->xdelete('article', $v[0], $v[1]);
                if($err) $err_num++;
            }

            if($err_num) {
                E(1, $err_num.' 篇文章删除失败！');
            }else{
                E(0, '删除成功！');
            }
        }else{
            E(1, '参数不能为空！');
        }
    }

	// hook admin_user_control_after.php
}
